/**
 * 
 */
package com.hk.migration.cannal.process;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.alibaba.otter.canal.protocol.CanalEntry.EventType;
import com.hk.migration.cannal.CanalData;
import com.hk.migration.cannal.CanalProcess;
import com.hk.migration.cannal.DataHandler;
import com.hk.migration.db.craftsman.BizOrder;
import com.hk.migration.db.hk.FundPackage;
import com.hk.migration.db.hk.UserInfo;
import com.hk.migration.mapper.craftsman.BizOrderMapper;
import com.hk.migration.mapper.hk.UserInfoMapper;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

/**
 * @Description: 购买套餐订单表同步
 * @author loujianfeng
 * @date Nov 3, 2020
 *
 */
@Slf4j
@Component
@AllArgsConstructor
public class FundPackageProcess implements CanalProcess {

	private final BizOrderMapper bizOrderMapper;
	
	private final UserInfoMapper userInfoMapper;
	
	@Override
	public boolean apply(String tableName, EventType eventType) {
		return "fund_package".equals(tableName);
	}

	@Override
	public boolean processData(List<CanalData> beforeCanalDataList, List<CanalData> afterCanalDataList,
			EventType eventType) {
		FundPackage fundPackageAfter = DataHandler.convertToBean(afterCanalDataList, FundPackage.class);
		if (eventType == CanalEntry.EventType.INSERT){
			BizOrder bizOrder = new BizOrder();
			bizOrder.setCompanyId(1L);
			bizOrder.setOrderId(fundPackageAfter.getOrderId());
			bizOrder.setStudentId(Long.valueOf(fundPackageAfter.getUid()));
			bizOrder.setNeedContract(fundPackageAfter.getNeedContract());
			bizOrder.setNeedInvoice(fundPackageAfter.getNeedInvoice());
			bizOrder.setStudentName(fundPackageAfter.getContractName());
			UserInfo userInfo = userInfoMapper.selectByPrimaryKey(fundPackageAfter.getUid());
			bizOrder.setContractAddress(userInfo.getAddress());
			bizOrder.setExpectedAmount(fundPackageAfter.getAmount());
			bizOrder.setDiscountAmount(fundPackageAfter.getDiscount());
			bizOrder.setPayableAmount(fundPackageAfter.getPayAmount());
			if(fundPackageAfter.getChannel()==0) { //0:新签，1:转介绍，2:补差，3:续费
				bizOrder.setOrderType("INIT");
			}else if(fundPackageAfter.getChannel()==1) {
				bizOrder.setOrderType("INVITE");
			}else if(fundPackageAfter.getChannel()==2) {
				bizOrder.setOrderType("RENEW");
			}else {
				bizOrder.setOrderType("SPREAD");
			}
			bizOrder.setAssociatedOrderId("");
			bizOrder.setOrderUserId(Long.valueOf(fundPackageAfter.getCreatorId()));
			bizOrder.setOrderUserAccount(fundPackageAfter.getCreatorName());
			if(!ObjectUtils.isEmpty(fundPackageAfter.getAuditId())) {
				bizOrder.setAuditorId(Long.valueOf(fundPackageAfter.getAuditId()));
				bizOrder.setAuditedTime(fundPackageAfter.getAuditTime());
				bizOrder.setAuditorAccount(fundPackageAfter.getAuditName());
			}
			bizOrder.setRemark(fundPackageAfter.getDesc());
			if(fundPackageAfter.getState()==0) {//0：待支付 1，支付成功 2:订单已添加,3:关闭,4:部分支付,5:全部支付,6:财务审核
				bizOrder.setOrderStatus("NOT_PAY");
			}else if(fundPackageAfter.getState()==3) {
				bizOrder.setOrderStatus("CANCEL");
			}else if(fundPackageAfter.getState()==4) {
				bizOrder.setOrderStatus("PARTIAL_PAYMENT");
			}else if(fundPackageAfter.getState()==5) {
				bizOrder.setOrderStatus("FULL_PARTIAL");
			}
			bizOrder.setAuditedStatus("PENDING");
			bizOrder.setCreateTime(new Date(fundPackageAfter.getCreateTime()*1000L));
			bizOrder.setCreateUserId(Long.valueOf(fundPackageAfter.getCreatorId()));
			bizOrder.setCreateAccount(fundPackageAfter.getCreatorName());
			bizOrder.setHkId(Long.valueOf(fundPackageAfter.getId()));
			bizOrderMapper.insertUseGeneratedKeys(bizOrder);
			
        }else if(eventType == CanalEntry.EventType.UPDATE){
        	Example example = new Example(BizOrder.class);
        	example.createCriteria().andEqualTo("companyId", 1L).andEqualTo("orderId", fundPackageAfter.getOrderId());
        	BizOrder bizOrder = new BizOrder();
        	bizOrder.setStudentId(Long.valueOf(fundPackageAfter.getUid()));
			bizOrder.setNeedContract(fundPackageAfter.getNeedContract());
			bizOrder.setNeedInvoice(fundPackageAfter.getNeedInvoice());
			bizOrder.setStudentName(fundPackageAfter.getContractName());
			UserInfo userInfo = userInfoMapper.selectByPrimaryKey(fundPackageAfter.getUid());
			bizOrder.setContractAddress(userInfo.getAddress());
			bizOrder.setExpectedAmount(fundPackageAfter.getAmount());
			bizOrder.setDiscountAmount(fundPackageAfter.getDiscount());
			bizOrder.setPayableAmount(fundPackageAfter.getPayAmount());
			bizOrder.setAssociatedOrderId("");
			bizOrder.setOrderUserId(Long.valueOf(fundPackageAfter.getCreatorId()));
			bizOrder.setOrderUserAccount(fundPackageAfter.getCreatorName());
			if(!ObjectUtils.isEmpty(fundPackageAfter.getAuditId())) {
				bizOrder.setAuditorId(Long.valueOf(fundPackageAfter.getAuditId()));
				bizOrder.setAuditedTime(fundPackageAfter.getAuditTime());
				bizOrder.setAuditorAccount(fundPackageAfter.getAuditName());
				
				bizOrder.setAuditedStatus("PASSED");
			}
			bizOrder.setRemark(fundPackageAfter.getDesc());
			if(fundPackageAfter.getState()==0) {//0：待支付 1，支付成功 2:订单已添加,3:关闭,4:部分支付,5:全部支付,6:财务审核
				bizOrder.setOrderStatus("NOT_PAY");
			}else if(fundPackageAfter.getState()==3) {
				bizOrder.setOrderStatus("CANCEL");
			}else if(fundPackageAfter.getState()==4) {
				bizOrder.setOrderStatus("PARTIAL_PAYMENT");
			}else if(fundPackageAfter.getState()==5) {
				bizOrder.setOrderStatus("FULL_PARTIAL");
			}
			bizOrder.setModifiedTime(fundPackageAfter.getGmtModified());
			bizOrder.setModifiedUserId(Long.valueOf(fundPackageAfter.getModifierId()));
			bizOrder.setModifiedAccount(fundPackageAfter.getModifierName());
			bizOrderMapper.updateByExampleSelective(bizOrder, example);
        }
		return false;
	}

}
